﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using MedicalAssistant.core.dao;

namespace MedicalAssistant.core.dao.xml
{
    abstract class XMLGenericDAO<T,ID> : GenericDAO<T,ID>
    {
        public abstract T FindById(ID id);

        public List<T> GetAll()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<T>));
            string path = GetXmlPath();
            StreamReader sr = new StreamReader(path);
            List<T> all = (List<T>)ser.Deserialize(sr);
            sr.Close();
            return all;
        }


        public abstract T Save(T entity);

        public abstract void Delete(T entity);

        protected void MakePersistent(List<T> all)
        {
            XmlSerializer ser = new XmlSerializer(all.GetType());
            string path = GetXmlPath();
            StreamWriter sw = new StreamWriter(path);
            ser.Serialize(sw, all);
            sw.Close();
        }

        protected abstract string GetXmlPath();

    }
}